
#include "PendingDB.h"

PendingDB::~PendingDB()
{
}

std::vector<char *> *PendingDB::GetAllPendingUserRequests()
{
   return sqlScanAllText("select * from 'user_requests';");
}

void PendingDB::RemovePendingUserRequest(const char *name)
{
   sqlInstantExecf("delete from 'user_requests' where name='%s';", name);
}

void PendingDB::AddNewUserRequest(const char *name, const char *host, const char *date, const char *publickey, const char *contactkey)
{
   // If there was a collision, it would have been spotted previously
   sqlInstantExecf("insert into user_requests (name, host, date, keydata, keyused) values ('%s', '%s', '%s', '%s', '%s');",
      name, host, date, publickey, contactkey);
}

std::vector<char *> *PendingDB::GetAllPendingHostKeyUpdates()
{
   return sqlScanAllText("select * from 'host_key_updates';");
}

void PendingDB::RemovePendingHostKeyUpdate(const char *hostname)
{
   sqlInstantExecf("delete from 'host_key_updates' where hostname='%s';", hostname);
}

void PendingDB::AddNewHostKeyUpdateRequest(const char *hostname, const char *oldkeyfingerprint, const char *newkeyfingerprint)
{
   sqlInstantExecf("insert or replace into host_key_updates (hostname, oldkeyfingerprint, newkeyfingerprint) values ('%s', '%s', '%s');",
      hostname, oldkeyfingerprint, newkeyfingerprint);
}

void PendingDB::CreateTablesIfNecessary()
{
   ensureTablesAreCreated(2,
      "user_requests", "(name char(64) primary key not null, host char(128), date char(64), keydata char(4096), keyused char(64))",
      "host_key_updates", "(hostname char(256) primary key not null, oldkeyfingerprint char(64), newkeyfingerprint char(64))");
}
